		package com.erpanderp.wam.ac.view.project;
		
		import java.io.Serializable;
		import java.util.ArrayList;
		import java.util.Date;
		import java.util.HashMap;
		import java.util.List;
		import java.util.Map;
		
		import javax.faces.application.FacesMessage;
		import javax.faces.bean.ManagedBean;
		import javax.faces.bean.ViewScoped;
		import javax.faces.context.FacesContext;
		import javax.faces.event.AjaxBehaviorEvent;
		import javax.servlet.ServletContext;
		import javax.servlet.http.HttpServletRequest;
		import javax.servlet.http.HttpSession;
		import javax.swing.SwingWorker;
		
		import com.erpanderp.wam.ac.model.entity.project.Journey;
		import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
		import com.erpanderp.wam.ac.model.entity.project.ProjectAssignmentDetail;
		import com.erpanderp.wam.ac.model.entity.project.VISA;
		import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpenseDetails;
		import com.erpanderp.wam.ac.model.service.project.ProjectAssignmentDAO;
		import com.erpanderp.wam.adm.model.entity.Service;
		import com.erpanderp.wam.tm.model.service.RRFDAO;
		import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
		import com.erpanderp.wam.usm.model.entity.user.Employee;
		import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
		import com.erpanderp.wam.usm.model.entity.user.NewJoinee;
		import com.erpanderp.wam.usm.model.entity.user.PassportAndPANDetails;
		import com.erpanderp.wam.usm.model.entity.user.WAMUser;
		import com.erpanderp.wam.usm.model.service.projectandassignment.ProjectDAO;
		import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
		import com.erpanderp.wam.util.CommonLookup;
		import com.erpanderp.wam.util.EmailUtil;
		import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMHostManager;
		@ManagedBean
		@ViewScoped
		public class CreateInternationalProjectAssignmentBean implements Serializable {
		private static final long serialVersionUID = 1L;
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
		
		ProjectDAO projectDAO = new ProjectDAO();
		ProjectAssignmentDAO projectAssignmentDAO = new ProjectAssignmentDAO();
		RRFDAO rrfDAO = new RRFDAO();
		EmployeeAndReportingDAO employeeAndReportingDAO = new EmployeeAndReportingDAO();
		
		private boolean FORM_VALID;
		
		private ProjectAssignment projectAssignment = new ProjectAssignment();
		private ProjectAssignmentDetail projectAssignmentDetail = new ProjectAssignmentDetail();
		private Journey onwardJourney = new Journey();
		private Journey returnJourney = new Journey();
		private VISA visa = new VISA();
		private Employee employee = new Employee();
		private NewJoinee newJoinee = new NewJoinee();
		private PassportAndPANDetails passportAndPANDetails = new PassportAndPANDetails();
		private EmployeeAndReporting employeeAndReporting = new EmployeeAndReporting();
		
		
		private List<Project> projects = new ArrayList<Project>();
		private List<PassportAndPANDetails> passportAndPANDetailsList = new ArrayList<PassportAndPANDetails>();
		private Map<String, Long> projectsMap = new HashMap<String, Long>();
		private List<Journey> onwardJourneyDetails = new ArrayList<Journey>();
		private List<Journey> returnJourneyDetails = new ArrayList<Journey>();
		private List<Journey> onwardJournies = new ArrayList<Journey>();
		private List<Journey> returnJournies = new ArrayList<Journey>();
		
		
		private Long selectedProjectId;
		private Project selectedProject= new Project();
		
		private WAMUser wamUser = new WAMUser();
		private String InternationalProjectAssignmentCode;
		private String employeeId;
		private String passportNumber;
		private String contactNumber="";
		private String approvedBy="";
		private Float advanceAmount;
		private Float roomRentAdvance;
		private Date createDate;
		private String additionals;
		String EMAIL_MESSAGE;
		
		private Employee reportingManager = new Employee();
		private Long reportingManagerId;
		private EmailUtil emailUtil = new EmailUtil();
		
		public CreateInternationalProjectAssignmentBean()
		{
		setWamUser((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		InternationalProjectAssignmentCode=CommonLookup.getAutoGeneratedInternationalProjectAssignmentCode(wamUser.getEmployee().getEmployeeCode());
		System.out.println("InternationalProjectAssignmentId.........."+InternationalProjectAssignmentCode);
		setCreateDate(new Date());
		employee.setFirstName(wamUser.getEmployee().getFirstName());
		employee.setLastName(wamUser.getEmployee().getLastName());
		setEmployeeId(wamUser.getEmployee().getEmployeeCode());
		//wamUser.getEmployee().getNewJoinee().getPassportAndPANDetails();
		setPassportNumber("AD45567H890");
//		passportAndPANDetailsList.addAll(wamUser.getEmployee().getNewJoinee().getPassportAndPANDetails());
//		for(PassportAndPANDetails passportAndPANDetails : passportAndPANDetailsList){
//		setPassportNumber(passportAndPANDetails.getPassportNumber());
//			
//		}
		setContactNumber(wamUser.getEmployee().getTelephone());
		setApprovedBy(CommonLookup.PROJECTASSIGNMENT_DOMESTIC_INTERNATIONAL_APPROVER);
		
		//reportingManager = rrfDAO.getReportingManager(wamUser.getEmployee().getEmployeeId());
		employeeAndReporting = employeeAndReportingDAO.getEmployeeReportingDetail(wamUser.getEmployee().getEmployeeId());
		  
		System.out.println("reportingManager.........."+employeeAndReporting);
		 
		//Getting list of projects
		projects = projectDAO.getAllProjects();
		  for(Project project :projects)
		  {
			  projectsMap.put(project.getProjectName(), project.getProjectId());
		  }
		}
		
		public void projectChangeListener(AjaxBehaviorEvent e) {
			
		if(selectedProjectId == null || selectedProjectId == 0)
		{
		
		}
		else
		{
			selectedProject = projectDAO.getProjectByProjectID(selectedProjectId);
		}
		 }
		
		public String reinitOnward() {  
		    
		 System.out.println("In reinitOnward.....");
		 
		 //projectAssignment = new ProjectAssignment();
		 onwardJourney.setCreateDate(new Date());
		 onwardJourney.setCreatedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
		 onwardJourney.setJourneyType(CommonLookup.JOURNEY_TYPE_ONWARD);
		 onwardJourney.setStatus(CommonLookup.STATUS_ACTIVE);
		 onwardJourney.setProjectAssignment(projectAssignment);
		 projectAssignment.getOnwardJourneyDetails().add(onwardJourney);
		 System.out.println("projectAssignment........"+projectAssignment.getOnwardJourneyDetails().add(onwardJourney));
		 onwardJourney = new Journey();
		 //
			 return null;  
			 } 
		public void removeFromOnwardJourneyList()
		{
			System.out.println("IN REMOVE METHOD");
		}
		
		public String reinitReturn() {  
		   
		    
		System.out.println("In reinitReturn.....");
		//projectAssignment = new ProjectAssignment();
		returnJourney.setCreateDate(new Date());
		returnJourney.setCreatedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
		returnJourney.setJourneyType(CommonLookup.JOURNEY_TYPE_RETURN);
		returnJourney.setStatus(CommonLookup.STATUS_ACTIVE);
		returnJourney.setProjectAssignment(projectAssignment);
		projectAssignment.getReturnJourneyDetails().add(returnJourney);
		System.out.println("projectAssignment........"+projectAssignment.getReturnJourneyDetails().add(returnJourney));
		returnJourney = new Journey();
		//
		return null;  
		} 
		public void removeFromReturnJourneyList()
		{
			System.out.println("IN REMOVE METHOD");
		}
		
		//SAVE METHOD
		
		public String saveInternationalProjectAssignmentAction() 
		{
		validateForm();
			if(FORM_VALID){
			emailUtil = new EmailUtil();
			projectAssignment.setProjectAssignmentCode(InternationalProjectAssignmentCode);
			projectAssignment.setEmployee(wamUser.getEmployee());
			visa.setEmployee(wamUser.getEmployee());
			projectAssignment.setContactNumber(wamUser.getEmployee().getTelephone());
			projectAssignment.setRecommendedBy(selectedProject.getOwner().getFirstName() +" "+ selectedProject.getOwner().getLastName());
			projectAssignment.setApprovedBy(CommonLookup.PROJECTASSIGNMENT_DOMESTIC_INTERNATIONAL_APPROVER);
			projectAssignment.setProjectManager(employeeAndReporting.getProjectManager());
			projectAssignment.setRecommendationStatus(CommonLookup.PROJECT_ASSIGNMENT_INPROCESS_STATUS);
 			projectAssignment.setApprovalStatus(CommonLookup.PROJECT_ASSIGNMENT_INPROCESS_STATUS);
 			projectAssignment.setProject(selectedProject);
 			projectAssignment.setProjectType(CommonLookup.PROJECT_ASSIGNMENT_TYPE_INTERNATIONAL);
 			projectAssignment.setCreateDate(new Date());
 			onwardJourney.setCreateDate(new Date());
 			returnJourney.setCreateDate(new Date());
 			visa.setCreateDate(new Date());
 			projectAssignment.setCreatedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
 			onwardJourney.setCreatedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
 			returnJourney.setCreatedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
 			onwardJourney.setJourneyType(CommonLookup.JOURNEY_TYPE_ONWARD);
 			returnJourney.setJourneyType(CommonLookup.JOURNEY_TYPE_RETURN);
 			visa.setCreatedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
 			projectAssignment.setStatus(CommonLookup.STATUS_ACTIVE);
 			visa.setStatus(CommonLookup.STATUS_ACTIVE);
 			projectAssignment.getVisaDetails().add(visa);
 			
 			if(projectAssignmentDAO.createInternationalProjectAssignment(projectAssignment) != null){
 				System.out.println("AT CALLER METHOD: " + sendMessage());
 				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, projectAssignment.getProjectAssignmentId() + " ProjectAssignment created successfully.", ""));
 				projectAssignment = new ProjectAssignment();
 			}else{
 				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Failed to create ProjectAssignment.", ""));
 			}
 		
			}
		else
		{
			
		}
		System.out.println("In save ProjectAssignment");
		return null;
		}
		
		private void validateForm(){
			System.out.println("validateForm.....................");
		FORM_VALID = true;
		if(ValidationUtil.isEmpty(projectAssignment.getEmergencycontactNumber(), "Emergency Contact Number ", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		System.out.println("selectedProjectId..........."+selectedProjectId);
		if(ValidationUtil.isEmpty(selectedProjectId.toString() , "Project", null, FacesContext.getCurrentInstance()) != null)
		{
		FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(String.valueOf(visa.getDailyAllowance()), "Daily Allowance(INR) ", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		
		System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);
		}
		
		//Email Functionality
		public String sendMessage(){
			Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
			String domainName = context.getInitParameter("domainName");
			if(hostsMap.get(CommonLookup.DEV_EMAIL) == null){
				String[] to = {selectedProject.getOwner().getEmail()};
				emailUtil.setRecieverAddresses(to);
			}else{
				String[] to = {hostsMap.get(CommonLookup.DEV_EMAIL)};
				emailUtil.setRecieverAddresses(to);
			} 
			String requestURL = "ac/project/approveinternationalprojectassignment.html";
			String privateLink = "http://" + hostsMap.get(CommonLookup.PRIVATEHOST) + "/" + domainName + "/" + requestURL+"?projectAssignmentId="+projectAssignment.getProjectAssignmentId();
			String publicLink = "http://" + hostsMap.get(CommonLookup.PUBLICHOST) + "/"	+ domainName + "/" + requestURL+"?projectAssignmentId="+projectAssignment.getProjectAssignmentId();
			String messageString = "Dear " + selectedProject.getOwner().getFirstName() + " " + selectedProject.getOwner().getLastName() + ",<br /><br />" + 
						"<a style='color:#FF9933;font-weight: bold;' href=\""+
						privateLink + "\">" + "Please click here to approve/reject international project assignment"+" "+projectAssignment.getProjectAssignmentCode() + "</a /><br /><br />"+
						"(Employees who are in Hyderabad office)"+"<br /><br />"+
						"<a style='color:#FF9933;font-weight: bold;' href=\""+
						publicLink + "\">" + "Please click here to approve/reject international project assignment"+" "+projectAssignment.getProjectAssignmentCode() + "</a /><br />"+
						"(Out side of Hyderabad office)"+"<br /><br />"+
		
					    "Thanks & Regards, <br />"
					    + CommonLookup.WAM_DEVTEAM_SIGNATURE;
			emailUtil.setMessage(messageString);
			emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
			emailUtil.setSubject("International project assignment request-"+projectAssignment.getProjectAssignmentCode());
		
			SwingWorker worker = new SwingWorker<String, Object>() {
			@Override
			protected String doInBackground() throws Exception {
				EMAIL_MESSAGE = emailUtil.processAndSendEmail();
				return null;
			}
	
			@Override
			protected void done() {
					System.out.println("DONE " + EMAIL_MESSAGE);		
			}
			};
			worker.execute();
			System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
			return EMAIL_MESSAGE;
		}
		
		//GETTERS AND SETTERS
		public HttpSession getSession() {
			return session;
		}
		public void setSession(HttpSession session) {
			this.session = session;
		}
		public HttpServletRequest getRequest() {
			return request;
		}
		public void setRequest(HttpServletRequest request) {
			this.request = request;
		}
		public ServletContext getContext() {
			return context;
		}
		public void setContext(ServletContext context) {
			this.context = context;
		}
		public ProjectDAO getProjectDAO() {
			return projectDAO;
		}
		public void setProjectDAO(ProjectDAO projectDAO) {
			this.projectDAO = projectDAO;
		}
		public ProjectAssignmentDAO getProjectAssignmentDAO() {
			return projectAssignmentDAO;
		}
		public void setProjectAssignmentDAO(ProjectAssignmentDAO projectAssignmentDAO) {
			this.projectAssignmentDAO = projectAssignmentDAO;
		}
		public RRFDAO getRrfDAO() {
			return rrfDAO;
		}
		public void setRrfDAO(RRFDAO rrfDAO) {
			this.rrfDAO = rrfDAO;
		}
		public boolean isFORM_VALID() {
			return FORM_VALID;
		}
		public void setFORM_VALID(boolean fORM_VALID) {
			FORM_VALID = fORM_VALID;
		}
		public ProjectAssignment getProjectAssignment() {
			return projectAssignment;
		}
		public void setProjectAssignment(ProjectAssignment projectAssignment) {
			this.projectAssignment = projectAssignment;
		}
		public ProjectAssignmentDetail getProjectAssignmentDetail() {
			return projectAssignmentDetail;
		}
		public void setProjectAssignmentDetail(
				ProjectAssignmentDetail projectAssignmentDetail) {
			this.projectAssignmentDetail = projectAssignmentDetail;
		}
		public Employee getEmployee() {
			return employee;
		}
		public void setEmployee(Employee employee) {
			this.employee = employee;
		}
		public EmployeeAndReporting getEmployeeAndReporting() {
			return employeeAndReporting;
		}
		public void setEmployeeAndReporting(EmployeeAndReporting employeeAndReporting) {
			this.employeeAndReporting = employeeAndReporting;
		}
		public List<Project> getProjects() {
			return projects;
		}
		public void setProjects(List<Project> projects) {
			this.projects = projects;
		}
		public Map<String, Long> getProjectsMap() {
			return projectsMap;
		}
		public void setProjectsMap(Map<String, Long> projectsMap) {
			this.projectsMap = projectsMap;
		}
		public Long getSelectedProjectId() {
			return selectedProjectId;
		}
		public void setSelectedProjectId(Long selectedProjectId) {
			this.selectedProjectId = selectedProjectId;
		}
		public Project getSelectedProject() {
			return selectedProject;
		}
		public void setSelectedProject(Project selectedProject) {
			this.selectedProject = selectedProject;
		}
		public WAMUser getWamUser() {
			return wamUser;
		}
		public void setWamUser(WAMUser wamUser) {
			this.wamUser = wamUser;
		}
		
		public String getEmployeeId() {
			return employeeId;
		}
		public void setEmployeeId(String employeeId) {
			this.employeeId = employeeId;
		}
		public String getContactNumber() {
			return contactNumber;
		}
		public void setContactNumber(String contactNumber) {
			this.contactNumber = contactNumber;
		}
		public String getApprovedBy() {
			return approvedBy;
		}
		public void setApprovedBy(String approvedBy) {
			this.approvedBy = approvedBy;
		}
		public Float getAdvanceAmount() {
			return advanceAmount;
		}
		public void setAdvanceAmount(Float advanceAmount) {
			this.advanceAmount = advanceAmount;
		}
		public Float getRoomRentAdvance() {
			return roomRentAdvance;
		}
		public void setRoomRentAdvance(Float roomRentAdvance) {
			this.roomRentAdvance = roomRentAdvance;
		}
		public Date getCreateDate() {
			return createDate;
		}
		public void setCreateDate(Date createDate) {
			this.createDate = createDate;
		}
		public String getAdditionals() {
			return additionals;
		}
		public void setAdditionals(String additionals) {
			this.additionals = additionals;
		}
		public String getEMAIL_MESSAGE() {
			return EMAIL_MESSAGE;
		}
		public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
			EMAIL_MESSAGE = eMAIL_MESSAGE;
		}
		public Employee getReportingManager() {
			return reportingManager;
		}
		public void setReportingManager(Employee reportingManager) {
			this.reportingManager = reportingManager;
		}
		public Long getReportingManagerId() {
			return reportingManagerId;
		}
		public void setReportingManagerId(Long reportingManagerId) {
			this.reportingManagerId = reportingManagerId;
		}
		public EmailUtil getEmailUtil() {
			return emailUtil;
		}
		public void setEmailUtil(EmailUtil emailUtil) {
			this.emailUtil = emailUtil;
		}
		public static long getSerialversionuid() {
			return serialVersionUID;
		}
		
		
		public String getInternationalProjectAssignmentCode() {
			return InternationalProjectAssignmentCode;
		}
		
		
		public void setInternationalProjectAssignmentCode(
				String internationalProjectAssignmentCode) {
			InternationalProjectAssignmentCode = internationalProjectAssignmentCode;
		}
		
		
		public String getPassportNumber() {
			return passportNumber;
		}
		
		
		public void setPassportNumber(String passportNumber) {
			this.passportNumber = passportNumber;
		}
		public PassportAndPANDetails getPassportAndPANDetails() {
			return passportAndPANDetails;
		}
		
		
		public void setPassportAndPANDetails(PassportAndPANDetails passportAndPANDetails) {
			this.passportAndPANDetails = passportAndPANDetails;
		}
		
		
		public NewJoinee getNewJoinee() {
			return newJoinee;
		}
		
		
		public void setNewJoinee(NewJoinee newJoinee) {
			this.newJoinee = newJoinee;
		}
		
		
		public List<PassportAndPANDetails> getPassportAndPANDetailsList() {
			return passportAndPANDetailsList;
		}
		
		
		public void setPassportAndPANDetailsList(List<PassportAndPANDetails> passportAndPANDetailsList) {
			this.passportAndPANDetailsList = passportAndPANDetailsList;
		}
		
		
		public List<Journey> getOnwardJourneyDetails() {
			return onwardJourneyDetails;
		}
		
		public void setOnwardJourneyDetails(List<Journey> onwardJourneyDetails) {
			this.onwardJourneyDetails = onwardJourneyDetails;
		}
		
		public List<Journey> getReturnJourneyDetails() {
			return returnJourneyDetails;
		}
		
		public void setReturnJourneyDetails(List<Journey> returnJourneyDetails) {
			this.returnJourneyDetails = returnJourneyDetails;
		}
		
		public Journey getOnwardJourney() {
			return onwardJourney;
		}
		
		public void setOnwardJourney(Journey onwardJourney) {
			this.onwardJourney = onwardJourney;
		}
		
		public Journey getReturnJourney() {
			return returnJourney;
		}
		
		public void setReturnJourney(Journey returnJourney) {
			this.returnJourney = returnJourney;
		}
		
		public VISA getVisa() {
			return visa;
		}
		
		public void setVisa(VISA visa) {
			this.visa = visa;
		}
		public EmployeeAndReportingDAO getEmployeeAndReportingDAO() {
			return employeeAndReportingDAO;
		}
		
		public void setEmployeeAndReportingDAO(
				EmployeeAndReportingDAO employeeAndReportingDAO) {
			this.employeeAndReportingDAO = employeeAndReportingDAO;
		}
		
		public List<Journey> getOnwardJournies() {
			return onwardJournies;
		}
		
		public void setOnwardJournies(List<Journey> onwardJournies) {
			this.onwardJournies = onwardJournies;
		}
		
		public List<Journey> getReturnJournies() {
			return returnJournies;
		}
		
		public void setReturnJournies(List<Journey> returnJournies) {
			this.returnJournies = returnJournies;
		}
		
		}
